<?php

/*
 * Author: Quan Van Sinh
 * Email : qvsinh@gmail.com
 * Mobile: 0972405165
 */

class image {

    var $db;
    var $table;

    function image() {
        global $db;
        $this->db = $db;
        $this->table = "image";
    }

    function addNewImage($data) {
        if (!is_null($data) && is_array($data)) {
            if ($db->exec_insert($this->table, $data))
                return $db->mysql_insert_id();
        }
        return -1;
    }

    function loadElements($folder='default') {
        $element_dir = BASE_DIR . '/uploads/ecard/template/' . $folder;
        $count = 0;
        $arrFile = array();
        $handle = opendir($element_dir);
        if ($handle) {
            while (false !== ($file = readdir($handle))) {
                if (strpos($file, '.png', 1) || strpos($file, '.PNG', 1) || strpos($file, '.JPG', 1) || strpos($file, '.jpg', 1)) {
                    $FileObj = array();
                    $info = pathinfo($file);
                    $file_name = basename($file, '.' . $info['extension']);
                    $FileObj['src'] = '/uploads/ecard/template/' . $folder . '/' . $file;
                    $FileObj['name'] = $file_name;
                    $arrFile[] = $FileObj;
                }
            }
            closedir($handle);
        }
        return $arrFile;
    }

//longnt insert 22062011
    function determineImageScale($sourceWidth, $sourceHeight, $targetWidth, $targetHeight) {
        $scalex = $targetWidth / $sourceWidth;
        $scaley = $targetHeight / $sourceHeight;
        return min($scalex, $scaley);
    }

    function returnCorrectFunction($ext) {
        $function = "";
        switch ($ext) {
            case "png":
                $function = "imagecreatefrompng";
                break;
            case "jpeg":
                $function = "imagecreatefromjpeg";
                break;
            case "jpg":
                $function = "imagecreatefromjpeg";
                break;
            case "gif":
                $function = "imagecreatefromgif";
                break;
        }
        return $function;
    }

    function parseImage($ext, $img, $file = null) {
        switch ($ext) {
            case "png":
                imagepng($img, ($file != null ? $file : ''));
                break;
            case "jpeg":
                imagejpeg($img, ($file ? $file : ''), 90);
                break;
            case "jpg":

                imagejpeg($img, ($file ? $file : ''), 90);
                break;
            case "gif":
                imagegif($img, ($file ? $file : ''));
                break;
        }
    }

    function setTransparency($imgSrc, $imgDest, $ext) {

        if ($ext == "png" || $ext == "gif") {
            $trnprt_indx = imagecolortransparent($imgSrc);
            // If we have a specific transparent color
            if ($trnprt_indx >= 0) {
                // Get the original image's transparent color's RGB values
                $trnprt_color = imagecolorsforindex($imgSrc, $trnprt_indx);
                // Allocate the same color in the new image resource
                $trnprt_indx = imagecolorallocate($imgDest, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
                // Completely fill the background of the new image with allocated color.
                imagefill($imgDest, 0, 0, $trnprt_indx);
                // Set the background color for new image to transparent
                imagecolortransparent($imgDest, $trnprt_indx);
            }
            // Always make a transparent background color for PNGs that don't have one allocated already
            elseif ($ext == "png") {
                // Turn off transparency blending (temporarily)
                imagealphablending($imgDest, true);
                // Create a new transparent color for image
                $color = imagecolorallocatealpha($imgDest, 0, 0, 0, 127);
                // Completely fill the background of the new image with allocated color.
                imagefill($imgDest, 0, 0, $color);
                // Restore transparency blending
                imagesavealpha($imgDest, true);
            }
        }
    }

}

?>
